#ifndef __ALSA_IO_H__
#define __ALSA_IO_H__ 1

#include "sample_io.h"
#include <alsa/asoundlib.h>
#include <stdint.h>

class CAlsaIO : public ISampleIO
{
public:
	// for capture
	CAlsaIO(EAction pAction, unsigned int pChannels);

	// for playback
	CAlsaIO(EAction pAction, unsigned int pChannels,
			NAudioFormats::SStreamInfo pStreamInfo);

	// for capture and playback
	CAlsaIO(EAction pAction);
	virtual ~CAlsaIO();

	/* START methods inherited by ISampleIO */
	virtual int init(CPropertiesReader *pConfig);
	virtual int destroy();

	virtual int read(void** pData, EAction& pAction, int pTimeout = 1000);
	virtual int write(void* pData, const EAction& pAction, int pLength);

	virtual NAudioFormats::SStreamInfo getStreamInfo();
	/* END methods inherited by ISampleIO */

private:
	enum EInitError {
		INIT_NO_ERROR          = 0,

		// errors
		INIT_FAILURE           = 1,
		INIT_CAPTURE_ERROR     = 2,
		INIT_PLAYBACK_ERROR    = 3,
	};

	EInitError mError;
	EAction mAction;

	bool mPlaybackPrepareForFirstTime;
	unsigned int mPlaybackChannels;
	snd_pcm_t *mPlaybackHandle;

	bool mCapturePrepareForFirstTime;
	unsigned int mCaptureChannels;
	snd_pcm_t *mCaptureHandle;
	char *mCaptureBuffer;

	NAudioFormats::SStreamInfo mStreamInfo;
	unsigned int mBytePerSample;

	CAlsaIO(const CAlsaIO& );
	CAlsaIO& operator=(const CAlsaIO& );

	bool initPlayback();
	bool initCapture();
	unsigned int getBytePerSample();
	snd_pcm_format_t getAlsaSndPcmFormat();
};

#endif /* __ALSA_IO_H__ */
